/*
 * Created on 2005-11-15
 *
 */
package com.yl.cap.analyst.report.chart.maker;

import java.awt.Color;
import java.io.FileOutputStream;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.TextAnchor;

import com.yl.cap.analyst.report.chart.data.BarLineChartData;
import com.yl.cap.analyst.report.chart.data.ChartData;

public class ValueBarMaker extends ChartMaker
{
    class LabelGenerator extends StandardCategoryItemLabelGenerator
    {

        public String generateItemLabel(CategoryDataset categorydataset, int i,
                int j)
        {
            return categorydataset.getRowKey(i).toString();
        }

        LabelGenerator()
        {
        }
    }

    public void makeChart(String fileName, ChartData data) throws Exception
    {
        BarLineChartData chartData = (BarLineChartData) data;
        CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
                chartData.getRowKeys(), chartData.getColumnKeys(), chartData
                        .getData());
        JFreeChart jfreechart = ChartFactory.createBarChart(data.getCaption(),
                chartData.getXCaption(), chartData.getYCaption(), dataset,
                PlotOrientation.VERTICAL, true, false, false);
        jfreechart.setBackgroundPaint(Color.white);
        CategoryPlot categoryplot = jfreechart.getCategoryPlot();
        categoryplot.setBackgroundPaint(Color.lightGray);
        categoryplot.setDomainGridlinePaint(Color.white);
        categoryplot.setRangeGridlinePaint(Color.white);
        // IntervalMarker intervalmarker = new IntervalMarker(4.5D, 7.5D);
        // intervalmarker.setLabel("Target Range");
        // intervalmarker.setLabelFont(new Font("SansSerif", 2, 11));
        // intervalmarker.setLabelAnchor(RectangleAnchor.LEFT);
        // intervalmarker.setLabelTextAnchor(TextAnchor.CENTER_LEFT);
        // intervalmarker.setPaint(new Color(222, 222, 255, 128));
        // categoryplot.addRangeMarker(intervalmarker, Layer.BACKGROUND);
        NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();
        barrenderer.setDrawBarOutline(false);
        barrenderer.setItemMargin(0.1);
        // GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F,
        // Color.blue, 0.0F, 0.0F, Color.lightGray);
        // GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F,
        // Color.green, 0.0F, 0.0F, Color.lightGray);
        // GradientPaint gradientpaint2 = new GradientPaint(0.0F, 0.0F,
        // Color.red, 0.0F, 0.0F, Color.lightGray);
        // barrenderer.setSeriesPaint(0, gradientpaint);
        // barrenderer.setSeriesPaint(1, gradientpaint1);
        // barrenderer.setSeriesPaint(2, gradientpaint2);
        barrenderer.setItemLabelGenerator(new LabelGenerator());
        barrenderer.setItemLabelsVisible(true);
        ItemLabelPosition itemlabelposition = new ItemLabelPosition(
                ItemLabelAnchor.INSIDE12, TextAnchor.CENTER_RIGHT,
                TextAnchor.CENTER_RIGHT, -1.5707963267948966D);
        barrenderer.setPositiveItemLabelPosition(itemlabelposition);
        ItemLabelPosition itemlabelposition1 = new ItemLabelPosition(
                ItemLabelAnchor.OUTSIDE12, TextAnchor.CENTER_LEFT,
                TextAnchor.CENTER_LEFT, -1.5707963267948966D);
        barrenderer.setPositiveItemLabelPositionFallback(itemlabelposition1);
        CategoryAxis categoryaxis = categoryplot.getDomainAxis();
        categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
        // =============================================================
        FileOutputStream fos_jpg = null;
        try
        {
            fos_jpg = new FileOutputStream(fileName);
            ChartUtilities.writeChartAsJPEG(fos_jpg, 100, jfreechart, data
                    .getWidth(), data.getHeight(), null);
            // ChartUtilities.writeChartAsJPEG(fos_jpg,100,chart,400,300,null);
        }
        catch (Exception e)
        {
            fos_jpg.close();
            throw e;
        }
        fos_jpg.close();
    }

}
